<?php
namespace Weibo\Model;
use Common\Model\BaseModel;
class WeiboUserRelationModel extends BaseModel {

	/**
	 * 获取关注用户ID集合
	 * @author	Baiyu
	 * @param	number	$uid			用户ID
	 * @param	number	$offset			起始行
	 * @param	number	$limit			取几行
	 * @return	array
	 */
	public function getFollowed($uid, $offset = 0, $limit = 0){
		return $this -> _getRelationUsers('followed', $uid, $offset, $limit);
	}
	
	/**
	 * 获取粉丝用户ID集合
	 * @author	Baiyu
	 * @param	number	$uid			用户ID
	 * @param	number	$offset			起始行
	 * @param	number	$limit			取几行
	 * @return	array
	 */
	public function getFans($uid, $offset = 0, $limit = 0){
		return $this -> _getRelationUsers('fans', $uid, $offset, $limit);
	}
	
	/**
	 * 获取相互关注用户ID集合
	 * @author	Baiyu
	 * @param	number	$uid			用户ID
	 * @param	number	$offset			起始行
	 * @param	number	$limit			取几行
	 * @return	array:
	 */
	public function getFriends($uid, $offset = 0, $limit = 0){
		return $this -> _getRelationUsers('friends', $uid, $offset, $limit);
	}
	
	/**
	 * 私有业务方法,获取某个关系的用户ID集合
	 * @author	Baiyu
	 * @param	string	$relation		什么关系 followed|fans|friends
	 * @param	number	$uid			用户ID
	 * @param	number	$offset			起始行
	 * @param	number	$limit			取几行
	 * @return	array
	 */
	private function _getRelationUsers($relation, $uid, $offset = 0, $limit = 0){
		
		//申明变量
		$return = array();
		$where = array();
		
		//组织查询条件
		switch ($relation){
			case 'followed':
				$where['user_id'] = $uid;
				//$where['mutual'] = 0;
				break;
			case 'fans':
				$where['follow'] = $uid;
				//$where['mutual'] = 0;
				break;
			case 'friends':
				$where['user_id'] = $uid;
				$where['mutual'] = 1;
				break;
			default:
				break;
		}
		
		//进行查询
		if (!empty($where)) {
			$this -> where($where);
			
			if ($limit) {
				$this -> limit($offset, $limit);
			}
			
			$list = $this -> select();
			if (!empty($list)) {
				$uids = array();
				foreach ($list as $item){
					$uids[] = $relation == 'fans' ? $item['user_id'] : $item['follow'];
				}
				$return = $uids;
			}
		}
		
		//返回结果
		return $return;
	}
	
	/**
	 * 根据用户ID查询用户关系
	 * @author	Baiyu
	 * @param	array	$uid				用户ID
	 * @param	array	$target_user_ids	要查询与哪些用户的关系
	 * @return	array
	 */
	public function getRelationByUserids($uid, $target_user_ids = array()) {
		
		//申明变量
		$is_single = is_array($target_user_ids) ? 0 : 1;			//查询单用户还是多个用户
		$relations = call_user_func_array(function($user_ids){		//构建返回结构
			$return = array();
			foreach ($user_ids as $user_id) {
				$return[$user_id] = array(
					'is_followed' => 0,
					'is_fans' => 0,
					'is_friend' => 0,
				);
			}
			return $return;
		}, array((array)$target_user_ids));
		
		//查询条件
		$user_ids_where = $is_single ? $target_user_ids : array('IN', $target_user_ids);
		
		//查出两种类型的数据
		$followed = $this -> where(array('user_id' => $uid, 'follow' => $user_ids_where)) -> select();
		$fans = $this -> where(array('user_id' => $user_ids_where, 'follow' => $uid)) -> select();
		
		//根据关注方向取得关系数据
		foreach ($followed as $followed_item) {
			$relations[$followed_item['follow']]['is_followed'] = 1;
			$followed_item['mutual'] && $relations[$followed_item['follow']]['is_friend'] = 1;
		}
		
		//根据被关注方向取得关系数据
		foreach ($fans as $fans_item) {
			$relations[$fans_item['user_id']]['is_fans'] = 1;
			$fans_item['mutual'] && $relations[$fans_item['user_id']]['is_friend'] = 1;
		}
		
		return $is_single ? reset($relations) : $relations;
	}
}
